package com.kmproject.haifu.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DataHelper;
import com.jess.arms.utils.DeviceUtils;
import com.jess.arms.widget.alertview.AlertView;
import com.jess.arms.widget.alertview.AlertViewOnItemClickListener;
import com.jess.arms.widget.autolayout.AutoToolbar;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.AppConstant;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.DataCleanManager;
import com.kmproject.haifu.di.component.DaggerUserSetingComponent;
import com.kmproject.haifu.mvp.contract.UserSetingContract;
import com.kmproject.haifu.mvp.model.entity.beans.UserLoginBean;
import com.kmproject.haifu.mvp.presenter.UserSetingPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/23/2019 23:34
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class UserSetingActivity extends BaseActivity<UserSetingPresenter> implements UserSetingContract.View {

    @BindView(R.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.back_right_icon)
    ImageView backRightIcon;
    @BindView(R.id.back_right_txt)
    TextView backRightTxt;
    @BindView(R.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R.id.toolbar)
    AutoToolbar toolbar;
    @BindView(R.id.userSeting_bjgrzl_txt)
    TextView userSetingBjgrzlTxt;
    @BindView(R.id.userSeting_xgdlmm_txt)
    TextView userSetingXgdlmmTxt;
    @BindView(R.id.userSeting_qchc_size_txt)
    TextView userSetingQchcSizeTxt;
    @BindView(R.id.userSeting_qchc_lay)
    LinearLayout userSetingQchcLay;
    @BindView(R.id.userSeting_qchc_dqbb_txt)
    TextView userSetingQchcDqbbTxt;
    @BindView(R.id.userSeting_exit_btn)
    TextView userSetingExitBtn;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerUserSetingComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_user_seting; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        setBtnStutas();
        toolbar.setBackgroundColor(ArmsUtils.getColor(this,R.color.color_EDEDED));
        userSetingQchcDqbbTxt.setText("V "+DeviceUtils.getVersionName(this));

    }

    @Override
    public void showLoading() {
           loadingDialog.show();
    }

    @Override
    public void hideLoading() {
            loadingDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.userSeting_bjgrzl_txt, R.id.userSeting_xgdlmm_txt, R.id.userSeting_qchc_lay, R.id.userSeting_exit_btn})
    public void onClick(View view) {
        switch (view.getId()) {
//            case R.id.userSeting_houseShow_btn:
//                ArmsUtils.startActivity(HouseRentingShowMainActivity.class);
//                break;
//            case R.id.userSeting_test_btn:
//                ArmsUtils.startActivity(HousePublishMainActivity.class);
//                break;
            case R.id.userSeting_bjgrzl_txt:
                ArmsUtils.startActivity(UserSetEditInfoActivity.class);
                break;
            case R.id.userSeting_xgdlmm_txt:
                ArmsUtils.startActivityForResult(UserSetingUpdatePwdActivity.class,AppConstant.START_ACTIVITY_REQUEST_CODE);
                break;

            case R.id.userSeting_qchc_lay:
                showDialog();
                break;

            case R.id.userSeting_exit_btn:
                new AlertView("提示", "确定要退出登录？", "取消", new String[]{"确定"}, null, this, AlertView.Style.Alert, new AlertViewOnItemClickListener() {

                    @Override
                    public void onItemClick(Object o, int position) {
                        if (position == AlertView.CANCELPOSITION) {
                            //点击取消按钮

                        }else {
                            mPresenter.logout();
                        }
                    }
                }).show();

                break;
        }
    }
    public void showDialog(){
        new AlertView("提示", "确定要清除缓存？", "取消", new String[]{"确定"}, null, this, AlertView.Style.Alert, new AlertViewOnItemClickListener() {

            @Override
            public void onItemClick(Object o, int position) {
                if (position == AlertView.CANCELPOSITION) {
                    //点击取消按钮

                }else {
                    DataCleanManager.clearAllCache(UserSetingActivity.this);
                    try {
                        userSetingQchcSizeTxt.setText(DataCleanManager.getTotalCacheSize(UserSetingActivity.this));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
//                    showMessage("清除成功");
                    AppUtil.midToast(UserSetingActivity.this,"清除成功");
                }
            }
        }).show();
    }

    @Override
    public void logoutSuccess() {
        killMyself();
//        setBtnStutas();
    }

    public void setBtnStutas(){
        UserLoginBean userLoginBean=DataHelper.getDeviceData(this,AppConstant.LOGUSER_LOGIN_INFOIN);
        if (userLoginBean==null)
        {
            userSetingExitBtn.setVisibility(View.GONE);

            return;
        }
        userSetingExitBtn.setVisibility(View.VISIBLE);

    }

    @Override
    protected void onResume() {
        super.onResume();
        try {
            userSetingQchcSizeTxt.setText(DataCleanManager.getTotalCacheSize(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
